class Solution {
public:
    static constexpr int mod = 1000000007;
    int minAbsoluteSumDiff(vector<int>& nums1, vector<int>& nums2) {
        vector<int> ans(nums1);
        int n = nums1.size();
        
        sort(ans.begin(),ans.end());
        
        int Max = 0;
        int ret = 0;
        for(int i = 0;i < n;i++){
            int dif = abs(nums1[i] - nums2[i]);
            ret = (ret + dif) % mod;
            
            int j = lower_bound(ans.begin(),ans.end(),nums2[i])-ans.begin();
            
            if(j < n){
                Max = max(Max,dif - (ans[j] - nums2[i]));
            }
            if(j > 0){
                Max = max(Max,dif - (nums2[i] - ans[j-1]));
            }
        }
        
        return (ret - Max + mod) % mod ;
    }
};
